package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.zznj.xzjc.R;
import com.zznj.xzjc.activity.index.IndexWebActivity;
import com.zznj.xzjc.adapter.AreaAdapter;
import com.zznj.xzjc.adapter.DiQuAdapter;
import com.zznj.xzjc.adapter.index.AlkAdapter;
import com.zznj.xzjc.adapter.index.DictionaryAdapter;
import com.zznj.xzjc.adapter.index.SadsrAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.AlkData;
import com.zznj.xzjc.bean.index.DictionaryData;
import com.zznj.xzjc.bean.index.SadsrData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.AreaBean;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.entity.ZzjgBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.view.MaxHeightRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityAlk extends BaseActivity {


    Unbinder unbinder;


    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @BindView(R.id.ckjb_Linear)
    View ckjb_Linear;
    @BindView(R.id.ckjb_tv)
    TextView ckjb_tv;
    @BindView(R.id.ckjb_tv_xx)
    ImageView ckjb_tv_xx;

    @BindView(R.id.xzqhxx_Linear)
    View xzqh_linear;
    @BindView(R.id.xzqh_tv)
    TextView xzqh_tv;
    @BindView(R.id.xzqh_tv_xx)
    ImageView xzqh_tv_xx;



    @BindView(R.id.zheCeng)
    View zheCeng;
    @BindView(R.id.xzqhList)
    MaxHeightRecyclerView xzqhList;
    @BindView(R.id.dicList)
    MaxHeightRecyclerView dicList;


    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;

    @BindView(R.id.bottomLinear)
    View bottomLinear;
    @BindView(R.id.tishiImg)
    ImageView tishiImg;


    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_alk_layout;
    }

    private LoginBean user;

    @Override
    public void initView() {
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        setTitle("案例库");
        initArea();
        initDictionary();
        initSrl();
        init();
    }

    private List<AlkData> list;
    private AlkAdapter mAdapter;

    private void init() {
        list = new ArrayList<>();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new AlkAdapter(R.layout.gyss_item_alk, list);
        rec_main.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("path", HttpMethods.BASE_URL+"xzapi/caseLib/getCaseLibDetail?caseLibId="+list.get(position).getId());
                bundle.putString("title", "案例库详情");
                bundle.putString("id", list.get(position).getId());
                RxActivityTool.skipActivity(mContext, IndexWebActivity.class,bundle);
            }
        });
    }

    private DiQuAdapter areaAdapter;
    private List<String> areaList;
    private int selectAreaPosition = 0;
    private String districtCode = "";

    private void initArea() {
        areaList = new ArrayList<>();
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xzqhList.setLayoutManager(layoutManager);
        areaAdapter = new DiQuAdapter(R.layout.gyss_item_area, areaList);
        xzqhList.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectAreaPosition = position;
                areaAdapter.setSelectPosition(selectAreaPosition);
                if(areaList.get(position).contains("全部")){
                    districtCode = "";
                    xzqh_tv.setText("地区");
                }else{
                    districtCode = areaList.get(position);
                    xzqh_tv.setText(areaList.get(position));
                }

                zheCeng.setVisibility(View.GONE);
                xzqhList.setVisibility(View.GONE);
                xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                selectLevel = null;
                selectImg = null;

                refresh();
            }
        });
    }

    private String sex = "";
    private List<DictionaryData> ckjbList;
    private int selectCkjbPosition = 0;
    private DictionaryAdapter dicAdapter;


    private String caseType = "";
    private void initDictionary() {
        ckjbList = new ArrayList<>();
        DictionaryData qb = new DictionaryData();
        qb.setDtCode("");
        qb.setDtName("参照级别(全部)");

        DictionaryData xgal = new DictionaryData();
        xgal.setDtCode("xgal");
        xgal.setDtName("相关案例");

        DictionaryData zdxal = new DictionaryData();
        zdxal.setDtCode("zdxal");
        zdxal.setDtName("指导性案例");

        ckjbList.add(qb);
        ckjbList.add(xgal);
        ckjbList.add(zdxal);


        ((SimpleItemAnimator) dicList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        dicList.setLayoutManager(linearLayoutManager);
        dicAdapter = new DictionaryAdapter(R.layout.gyss_item_select_dictionary, ckjbList);
        dicList.setAdapter(dicAdapter);
        dicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectCkjbPosition = position;

                caseType = dicAdapter.getData().get(position).getDtCode();
                if(StringUtils.isEmpty(caseType)){
                    selectLevel.setText("参照级别");
                }else{
                    selectLevel.setText(dicAdapter.getData().get(position).getDtName());
                }

                selectLevel.setTextColor(getResources().getColor(R.color.color_333333));
                selectImg.setImageResource(R.mipmap.gyss_xiala_img);

                selectLevel = null;
                selectImg = null;

                dicList.setVisibility(View.GONE);
                zheCeng.setVisibility(View.GONE);
                refresh();
            }
        });
    }

    @Override
    protected void initData() {
        getArea();
        refresh();
    }

    private void getArea() {
        SubscriberOnNextListener<BaseBean<List<String>>> onNextListener = baseBean -> {
            areaList.addAll(baseBean.getResult());
            areaList.add(0,"地区(全部)");
            areaAdapter.notifyDataSetChanged();
            areaAdapter.setSelectPosition(selectAreaPosition);
        };
        HttpMethods.getInstance().getAreaList(new ProgressSubscriber<>(onNextListener, mContext, true));
    }

    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }

    private boolean isShowDialog = true;
    private int page = 0;
    private List<Object> lcjdListData = new ArrayList<>();

    public void refresh() {
        srlAll.setEnableLoadMore(true);
        isShowDialog = false;
        page = 1;
        list.clear();
        srlAll.setEnableLoadMore(true);
        lcjdListData.clear();
        bottomLinear.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
        getData();
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<List<AlkData>>> onNextListener = baseBean -> {
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }
            if (list.size() + baseBean.getResult().size() > 0 && baseBean.getResult().size() < 10) {
                bottomLinear.setVisibility(View.VISIBLE);
                if (srlAll != null) {
                    srlAll.setEnableLoadMore(false);
                }
            }
            list.addAll(baseBean.getResult());
            mAdapter.notifyDataSetChanged();


            if (mAdapter.getItemCount() == 0) {
                tishiImg.setVisibility(View.VISIBLE);
                srlAll.setVisibility(View.GONE);
                tishiImg.setImageResource(R.mipmap.gyss_zwnr_img);
            } else {
                tishiImg.setVisibility(View.GONE);
                srlAll.setVisibility(View.VISIBLE);
            }
        };


        FormBody requestBody = new FormBody.Builder()
                .add("caseType", caseType)
                .add("districtName", districtCode)
                .add("keyWords", searchEdit.getText().toString())
                .add("page", page+"")
                .build();
        HttpMethods.getInstance().searchCaseLib(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private TextView selectLevel;
    private ImageView selectImg;

    @OnClick({R.id.ckjb_Linear, R.id.xzqhxx_Linear, R.id.searchBtn,R.id.zheCeng})
    public void onClick(View v) {
        if (v.getId() == R.id.ckjb_Linear) {
            xzqhList.setVisibility(View.GONE);
            xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
            xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);
            if (selectLevel == ckjb_tv) {
                selectLevel.setTextColor(getResources().getColor(R.color.color_333333));
                selectImg.setImageResource(R.mipmap.gyss_xiala_img);
                zheCeng.setVisibility(View.GONE);
                dicList.setVisibility(View.GONE);
                selectLevel = null;
                selectImg = null;
            } else {
                dicAdapter.setNewData(ckjbList);
                dicAdapter.setSelectPosition(selectCkjbPosition);

                zheCeng.setVisibility(View.VISIBLE);
                dicList.setVisibility(View.VISIBLE);

                ckjb_tv.setTextColor(getResources().getColor(R.color.title_bg));
                ckjb_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectLevel = ckjb_tv;
                selectImg = ckjb_tv_xx;
            }
        } else if (v.getId() == R.id.xzqhxx_Linear) {
            dicList.setVisibility(View.GONE);
            ckjb_tv.setTextColor(getResources().getColor(R.color.color_333333));
            ckjb_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);
            if (selectLevel == xzqh_tv) {
                xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);
                zheCeng.setVisibility(View.GONE);
                xzqhList.setVisibility(View.GONE);

                selectLevel = null;
                selectImg = null;
            } else {
                areaAdapter.setNewData(areaList);
                areaAdapter.setSelectPosition(selectAreaPosition);

                zheCeng.setVisibility(View.VISIBLE);
                xzqhList.setVisibility(View.VISIBLE);

                xzqh_tv.setTextColor(getResources().getColor(R.color.title_bg));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectLevel = xzqh_tv;
                selectImg = xzqh_tv_xx;
            }
        }else if(v.getId() == R.id.searchBtn){
            refresh();
        }else if(v.getId() == R.id.zheCeng){

        }
    }
}
